<?php
$host = 'localhost';
$user = 'cs143';
$db = 'CS143';
$pass = '';
if (isset($_POST['submit']))
{
    $count = 0;
    $conn = new mysqli($host, $user, $pass, $db);
    if ($result = $conn->query($_POST['query']))
    {
        $first = true;
        $num_results = $result->num_rows;
        $fields = $result->fetch_fields();
        $num_fields = count($fields);
    }
}
?><!DOCTYPE html>
<html>
    <head>
        <title>CS143 Project 1B - Web Query Interface</title>
        <style type="text/css">
            html, body {
                margin: 0;
                padding: 0;
            }
            body {
                padding-bottom: 50px;
                background-color: #333;
            }
            label {display: block;}
            textarea {display: block;}
            div.container {
                width: 960px;
                height: 100%;
                display: block;
                position: relative;
                margin: 15px auto;
                background-color: #fff;
                border-radius: 25px;
            }
            div.container > h1 {
                display: block;
                position: relative;
                text-align: center;
                margin: 0px auto;
                padding: 15px 0px 30px 0px;
                font-family: Helvetica, Arial, sans-serif;
                font-weight: 400;
            }
            div.container > p {
                display: block;
                text-align: center;
                margin: 0px 0px 25px 0px;
                font-style: italic;
                font-family: Helvetica, Arial, sans-serif;
                color: #006633;
                font-weight: 200;
            }
            div.container label {
                font-family: Helvetica, Arial, sans-serif;
                font-weight: 300;
                font-size: 14px;
                text-align: center;
                margin-bottom: 15px;
            }
            div.container input#submit {
                display: block;
                width: 150px;
                padding: 5px 5px;
                margin: 15px auto;
            }
            div.container textarea {
                margin: 0 auto 0 auto;
                display: block;
                position: relative;
                width: 750px;
                font-family: monospace;
                font-size: 20px;
                text-align: center;
                padding-top: 65px;
                height: 90px;
                background-color: #000;
                color: #00FF00;
            }
            
            table {
                display: block;
                margin: 0 auto 25px auto;
                border: 3px black dashed;
            }
            td, th {text-align: center; border: none; margin: 0px; padding: 5px 0px;}
            tr.odd {background-color: #ccccff; }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Web Query Interface</h1>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <label for="query">Enter Query</label>
                <textarea name="query" id="query" rows="12" cols="100"><?php echo isset($query) ? $query : ''; ?></textarea>
                <input type="submit" value="Perform Query" name="submit" id="submit">
            </form>
            <?php if (isset($num_results)) { ?>
            <h1>Results</h1>
            <p>There were <?php echo $num_results; ?> rows returned by your query.</p>
            <table width="900px" cellspacing="0px">
                <thead>
                    <tr>
                        <?php foreach ($fields as $field) { ?>
                        <th width="<?php echo $first ? 900 - (floor(900/$num_fields)*($num_fields-1)) : floor(900/$num_fields); ?>px"><?php echo $field->table.'.'.$field->name;//.'<br>'.$field->type; ?></th>
                        <?php $first = false; } ?>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $result->fetch_assoc()) { ?>
                    <tr<?php echo $count%2==0 ? ' class="odd"' : '' ; ?>>
                        <?php foreach ($fields as $field) { ?>
                        <td><?php echo $row[$field->name]; ?></td>
                        <?php } ?>
                    </tr>
                    <?php $count++; } ?>
                </tbody>
            </table>
            <?php } ?>
        </div>
    </body>
</html>